#include <sxCuboid_Archetype.h>
#include <sxIntersections.h>

Sx::Graphics::Archetypes::Objects::Cuboid_Archetype::Cuboid_Archetype(const std::shared_ptr<RendererImplementation>& renderer) :
	GeometricObject_Archetype(std::make_shared<Cuboid>(), renderer, std::make_shared<StaticRepresentation>()) {
	
}

Sx::Graphics::Archetypes::Objects::Cuboid_Archetype::Cuboid_Archetype(const std::shared_ptr<RendererImplementation>& renderer, const Eigen::Vector3f& min, const Eigen::Vector3f& max) :
	GeometricObject_Archetype(std::make_shared<Cuboid>(min, max), renderer, std::make_shared<StaticRepresentation>()) {
	
}

Sx::Graphics::Archetypes::Objects::Cuboid_Archetype::Cuboid_Archetype(const std::shared_ptr<RendererImplementation>& renderer, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) :
	GeometricObject_Archetype(std::make_shared<Cuboid>(minX, minY, minZ, maxX, maxY, maxZ), renderer, std::make_shared<StaticRepresentation>()) {
	
}

Sx::Graphics::Archetypes::Objects::Cuboid_Archetype::~Cuboid_Archetype() {}

bool Sx::Graphics::Archetypes::Objects::Cuboid_Archetype::construct() {
	return true;
}

bool Sx::Graphics::Archetypes::Objects::Cuboid_Archetype::pick(const Eigen::Vector3f& pickSegmentStart, const Eigen::Vector3f& pickSegmentEnd, Eigen::Vector3f& intersectionPoint) {
	Eigen::Vector3f tp0, tp1;
	this->transformLineToLocal(pickSegmentStart, pickSegmentEnd, tp0, tp1);
	bool intersect = Math::Intersections::SegmentIntersectAxisAlignedBoundingBox(this->object->getMin(), this->object->getMax(), tp0, tp1, intersectionPoint);

	if ( intersect == true ) {
		this->select();
		return true;
	}
	else this->deselect();
	return false;
}

bool Sx::Graphics::Archetypes::Objects::Cuboid_Archetype::intersect(const Eigen::Vector3f& segment_p0, const Eigen::Vector3f& segment_p1, Eigen::Vector3f& intersectionPoint) {
	Eigen::Vector3f tp0, tp1;
	this->transformLineToLocal(segment_p0, segment_p1, tp0, tp1);
	bool intersect = Math::Intersections::SegmentIntersectAxisAlignedBoundingBox(this->object->getMin(), this->object->getMax(), tp0, tp1, intersectionPoint);

	if ( intersect == true ) return true;
	else return false;
}
